home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 2437 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: soap.news.pipex.net!pipex!usenet
  2. From: m.hendry@dial.pipex.com (Mathew Hendry)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: silly c problem
  5. Date: Wed, 31 Jan 96 14:29:27
  6. Organization: Private node.
  7. Distribution: world
  8. Message-ID: <19960131.4755A8.D114@an181.du.pipex.com>
  9. References: <f2c_9601302132@techtol.magic.mb.ca>
  10. NNTP-Posting-Host: an181.du.pipex.com
  11. X-Newsreader: TIN [AMIGA 1.3 950726BETA PL0]
  12.  
  13. The problem here is the use of pointers in printf() - integer arguments
  14. should _not_ normally be referenced with pointers when calling printf(). If
  15. you do so, what will be printed, logically enough, is the address at which the
  16. integer is stored, rather than the value of that integer.
  17.  
  18. Perhaps you are confused by the different calling conventions of printf() and
  19. scanf() - it is safe to pass by value to printf(), since the value does not
  20. need to be changed by printf(). scanf(), on the other hand, will alter the
  21. value and so must be passed a _pointer_ to that value.
  22.  
  23. Tahir Khawaja (tahir.khawaja@techtol.magic.mb.ca) wrote:
  24. : #include <stdio.h>
  25. : main()
  26. : {
  27. :   int num1, num2, res, ope, err;
  28. :   printf("Please input two number!\n");
  29. :   scanf("%d%d", &num1, &num2);
  30. :   printf("You entered %d and %d.\n\n", &num1, &num2);
  31.  
  32.     printf("You entered %d and %d.\n\n", num1, num2);
  33.  
  34. :   printf("And now the code for the operation!\n");
  35. :   printf("1=Add, 2=Subtract, 3=Multiply, 4=Divide\n");
  36. :   scanf("%d", &ope);
  37. :   printf("You entered %d.\n\n", &ope);
  38.  
  39.     printf("You entered %d.\n\n", ope);
  40.  
  41. :   err = 1;
  42. :   if(ope == 1)
  43. :     {
  44. :       res = num1 + num2;
  45. :       err = 0;
  46. :     }
  47. :   if(ope == 2)
  48. :     {
  49. :       res = num1 - num2;
  50. :       err = 0;
  51. :     }
  52. :   if(ope == 3)
  53. :     {
  54. :       res = num1 * num2;
  55. :       err = 0;
  56. :     }
  57. :   if(ope == 4)
  58. :     {
  59. :       res = num1 / num2;
  60. :       err = 0;
  61. :     }
  62. :   if(err == 1)
  63. :     printf("Wrong Code! Input only number 1 - 4!\n");
  64. :   else
  65. :     printf("The result is %d %d %d %d\n.", &num1, &num2, &res, &err);
  66.  
  67.       printf("The result is %d %d %d %d\n.", num1, num2, res, err);
  68.  
  69. : }
  70. : -------------------------------
  71. :      pretty simple right?
  72. : --------------------------------
  73. : Please input two number!
  74. : 2 2
  75. : You entered 2817020 and 2817016.
  76.  
  77. These are the _addresses_ of the two values entered.
  78.  
  79. : And now the code for the operation!
  80. : 1=Add, 2=Subtract, 3=Multiply, 4=Divide
  81. : 1
  82. : You entered 2817008.
  83.  
  84. Same thing again.
  85.  
  86. : The result is 2817020 2817016 2817012 2817004
  87. : ----------------------------------
  88.  
  89. Ditto.
  90.  
  91. :      so what am i doing wrong.  i'm an absolute beginner at c but not at
  92. : programming.  the books i have don't tell me any different way to do this.  im
  93. : compiling w/ 'gcc -noixemul -o math math.c'  also pretty simple.  do i need to
  94. : #include some more stuff?
  95.  
  96. No.
  97.  
  98. Hope this helps (TM).
  99.  
  100. -- Mat.
  101.